import java.util.HashMap;
import java.util.zip.CRC32;

public class Test {
    public static String convert(String s, int numRows) {
        char[] str = s.toCharArray();
        char[] ret = new char[s.length()];
        int j = 0;
        for (int i = 1; i <= numRows; i++) {
            int index = i-1;
            if(i == 1 || i == numRows) {
                while (index < s.length() && j < s.length()) {
                    System.out.println(index);
                    ret[j++] = str[index];
                    index += (numRows - 1) * 2==0?1:(numRows - 1) * 2;
                }
            } else {
                while (index < s.length()) {
                    ret[j++] = str[index];
                    index += (numRows - i) * 2;
                    if(index < s.length()) {
                        ret[j++] = str[index];
                        index += (i-1) * 2;
                    }
                }
            }
        }
        return String.valueOf(ret);
    }

    public static String countAndSay(int n) {
        n--;
        StringBuffer str = new StringBuffer("1");
        StringBuffer t = new StringBuffer();
        for (int i = 0; i < n; i++) {
            int index = 0;
            while(index<str.length()) {
                int count = 1;
                while(index + 1 < str.length() && str.charAt(index) == str.charAt(index+1)) {
                    index++;
                    count++;
                }
                t.append(count);
                t.append(str.charAt(index));
                index++;
            }
            str = t;
            t = new StringBuffer();
        }

        return str.toString();
    }

    public static int minNumberOfFrogs1(String croakOfFrogs) {
        String str = "croak";
        HashMap<Character, Integer> map = new HashMap<>();
        for(int i= 0; i<str.length(); i++) {
            map.put(str.charAt(i),0);
        }

        for (int i = 0; i < croakOfFrogs.length(); i++) {
            char ch = croakOfFrogs.charAt(i);
            if(ch == 'c') {
                if(map.get('k') == 0) {
                    map.put('c', map.getOrDefault('c',0) + 1); //map(c)++
                } else {
                    map.put('c',map.getOrDefault('c',0) + 1); //map(c)++
                    map.put('k',map.get('k') -1); // map(k)--
                }
            } else if(ch == 'r') {
                if(map.get('c') == 0) {
                    return -1;
                }
                map.put('c', map.get('c')-1);  //map(c)--
                map.put('r', map.getOrDefault('r',0)+1); //map(r)++

            } else if(ch == 'o') {
                if(map.get('r') == 0) {
                    return -1;
                }
                map.put('r', map.get('r')-1);  //map(r) --
                map.put('o',map.getOrDefault('o',0)+1); //map(o)++
            } else if(ch == 'a') {
                if(map.get('o') == 0) {
                    return -1;
                }
                map.put('o', map.get('o')-1); //
                map.put('a',map.getOrDefault('a',0) + 1);
            }else {
                if(map.get('a') == 0) {
                    return -1;
                }
                map.put('a', map.get('a')-1);
                map.put('k', map.getOrDefault('k',0) + 1);
            }
        }

        for(int i = 0; i<4; i++) {
            char ch = croakOfFrogs.charAt(i);
            if(map.get(ch) != 0) {
                return -1;
            }
        }
        return map.get('k');
    }

    public static int minNumberOfFrogs(String c) {
        char[] croakOfFrogs = c.toCharArray();
        String t = "croak";
        int n = t.length();
        int[] hash = new int[n];  //数组模拟哈希表
        // x 这个字符对应的下标
        HashMap<Character,Integer> map = new HashMap<>();
        for(int i = 0; i<c.length(); i++) {
            map.put(t.charAt(i),i);
        }

        for(char ch:croakOfFrogs) {
            if(ch == t.charAt(0)) {
                if(hash[n-1] != 0) hash[n-1] --;
                hash[0]++;
            } else {
                int i = map.get(ch);
                if(hash[i-1] == 0) return -1;
                hash[i-1]--;
                hash[i]++;
            }
        }

        for(int i = 0; i< t.length() -1; i++) {
            if(hash[i] != 0) {
                return -1;
            }
        }

        return hash[n-1];
    }

    public static void main(String[] args) {
        int ret = minNumberOfFrogs("crcoakroak");
        System.out.println(ret);
    }

    public static void main2(String[] args) {
        String ret = countAndSay(1);
        System.out.println(ret);
    }

    public static void main1(String[] args) {
        String ret = convert("AB",1);
        System.out.println(ret);
    }
}
